Class SimplyPresentable::ActiveRecordUrlPresenter
In: vendor/plugins/simply_presentable/lib/simply_presentable/presenter/active_record_url.rb
Parent: SimplyPresentable::ActiveRecordPresenter

Methods

Constants

ROUTE_NAME_OPTIONS = [:name_prefix, :action]

Public Instance methods

Returns a url to show the collection view of the resource related to this object using a named (possibly nested) route.

Example:

  Foo.find(:first).collection_url
  => http://example.com/foos

Options: same as for url

[Source]

    # File vendor/plugins/simply_presentable/lib/simply_presentable/presenter/active_record_url.rb, line 67
67:   def collection_url(options = {})
68:     generate_url(options)
69:   end

Returns a url to show the edit view of the resource related to this object using a named (possibly nested) route. Example:

  Foo.find(:first).edit_url
  => http://example.com/foos/1;edit

Options: same as for url

[Source]

    # File vendor/plugins/simply_presentable/lib/simply_presentable/presenter/active_record_url.rb, line 43
43:   def edit_url(options = {})
44:     generate_url(options, :singular => true, :action => 'edit')
45:   end

Returns a url to show the new view of the resource related to this object using a named (possibly nested) route.

Example:

  Foo.find(:first).new_url
  => http://example.com/foos/new

Options: same as for url

[Source]

    # File vendor/plugins/simply_presentable/lib/simply_presentable/presenter/active_record_url.rb, line 55
55:   def new_url(options = {})
56:     generate_url(options, :singular => true, :new => true)
57:   end

Returns a url to show the resource related to this object using a named (possibly nested) route.

Example:

  map.resources(:foos) { |foo| foo.resources :bar }
  Foo.find(:first).url
  => http://example.com/foos/1
  Bar.find(:first).url
  => http://example.com/foos/1/bars/2

Options:

:name_prefix:A name prefix to use with the named routes:
    map.resources :bars, :name_prefix => 'foo'
    present(Bar.find(:first)).url(:name_prefix => 'foo')
:action:Select an action from the controller:
    map.resources :bars, :member => 'foo'
    present(Foo.find(:first)).url(:action => 'bar')
    => http://example.com/foos/1;bar

You can also provide the options expected in ActionController::Base#url_for:

  present(Foo.find(:first)).url(:host => 'baw', :baz => 'bam')
  => http://baw/foos/1?baz=bam

This assumes that the Bar class will have a method foo, that will return the foo object to be used for the named route. If your application does not follow this convention, you will need to override the protected url_params method in a sub class of this presenter that is used_for the model in question.

[Source]

    # File vendor/plugins/simply_presentable/lib/simply_presentable/presenter/active_record_url.rb, line 32
32:   def url(options = {})
33:     generate_url(options, :singular => true)
34:   end

Protected Instance methods

[Source]

    # File vendor/plugins/simply_presentable/lib/simply_presentable/presenter/active_record_url.rb, line 72
72:     def url_params(route_name)
73:       route = ActionController::Routing::Routes.named_routes[route_name.intern]
74:       named_route_keys = route.segments.select{ |s| s.respond_to? :key }.map(&:key).reverse
75:       returning url_params = {} do
76:         named_route_keys.inject(@presenter_proxy) do |record, url_param_key|
77:           belongs_to_association_name = url_param_key.to_s.gsub(/_id$/, '').gsub(/^id$/, 'to_param')
78:           url_params[url_param_key] = @presenter_proxy.send belongs_to_association_name
79:         end
80:       end
81:     end

[Validate]